(function($) {
$.fn.extend({
	divAlert:function(options){   
		var opts = $.extend({
			callback:function(){}
		},options);
		var top = $(this).offset().top;//获取控件top、left位置和width、height 
		var left = $(this).offset().left; 
		var height = $(this).height(); 
		var width = $(this).width()+3;
		var div_html = '<div id="page_alert_bg"></div>';
		
		switch(opts.style) {
			case 'form':
				div_html += '<div id="page_alert_confim" style="top:'+((height-130)/2)+'px;left:'+((width-308)/2)+'px;">';
				div_html += '<div class="top_close" id="top_close" title="close"></div>';
				div_html += '<form id="transferred_form" method="post" action="'+opts.formaction+'">';
				div_html += '<ul>';
				div_html += '<li>';
				div_html += '<span class="l">Paypal Account: </span>';
				div_html += '<span class="r"><input type="text" name="email" value="" /></span>';
				div_html += '</li>';
				div_html += '<li>';
				div_html += '<span class="l">Amount of cash: $</span>';
				div_html += '<span class="r"><input type="text" name="amount" value="" /></span>';
				div_html += '</li>';
				div_html += '<li>';
				div_html += '<span class="l">&nbsp;</span>';
				div_html += '<span class="r">The amount will be transferred to your paypal account in 2 days.</span>';
				div_html += '</li>';
				div_html += '<li>';
				div_html += '<span class="l">&nbsp;</span>';
				div_html += '<span class="r"><a href="javascript:void(0)" class="submit" id="transferred_form_sub"></a></span>';
				div_html += '</li>';
				div_html += '</ul>';
				div_html += '</form>';
				div_html += '</div>';
				$(this).append(div_html);
				$("#transferred_form_sub").click(function(){
					//check
					var re=/^[0-9a-zA-Z\-_\.]+@[0-9a-zA-Z\-_]+\.[a-zA-Z]+\.*[a-zA-Z]*$/;
					if (!re.test($('input[name="email"]').val())) {
						//$('input[name="email"]').val('Please input your correct email.');
						alert('Please input your correct email.');
						return false;
					}
					var re=/^[^0]\d*(\.\d{2})?$/;
					if (!re.test($('input[name="amount"]').val())) {
						//$('input[name="amount"]').val('Please input the amount of cash.');
						alert('Please input the amount of cash.');
						return false;
					}
					var old = $('#mycash').html();
					var amount = $('input[name="amount"]').val();
					if(amount > old) {
						//$('input[name="amount"]').val('The amount you input is not available.');
						alert('The amount you input is not available.');
						return false;
					}
									
					$.ajax({
						type: "get",
						url: "/sell-index",			
						success: function(data) {	
							
						}			
					})
					$.ajax({
						type: "post",
						url: "/sell-cash",		
						data: {email: $('input[name="email"]').val(), amount: $('input[name="amount"]').val()},
						success: function(data) {	
							switch (data) {
								case '5':		
									var old = $('#mycash').html();
									var amount = $('input[name="amount"]').val();									
									var newAmount = old - amount;
									newAmount = newAmount*Math.pow(10,2);
									newAmount = Math.round(newAmount+0.000000001);
									newAmount = newAmount/Math.pow(10,2); 
									$('#mycash').html(newAmount);
									$('#page_alert_confim').remove();
									$('#page_alert_bg').remove();
									$('div[ar="main"]').divAlert({style:'done',formaction:'/submit_url'});
									
									break;
								default:
									alert('error');
									break;
							}
						}			
					})
					return;
					$("#transferred_form").submit();
				});
				break;
			case 'done':
				div_html += '<div id="page_alert_done" style="top:'+((height-88)/2)+'px;left:'+((width-606)/2)+'px;"><span class="top_close" id="top_close" title="close"></span><span class="text">Done! The amount will be transferred to your paypal account in 2 days.</span></div>';
				$(this).append(div_html);
				break;
		}
		$("#page_alert_bg").click(function(){
			$(this).remove();
			switch(opts.style) {
				case 'form':
					$("#page_alert_confim").remove();
					break;
				case 'done':
					$("#page_alert_done").remove();
					break;
			}
		});
		$("#top_close").click(function(){
			$("#page_alert_bg").trigger('click');
		});
		
		return this.each(function(){   
			//
		});
	}
});

})(jQuery);